$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/baseworkiteminfo/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '工作项名称', name: 'workitemName', index: 'workitem_name', width: 80},
            {label: '交付物描述', name: 'deliveryGoods', index: 'deliveryGoods', width: 80},
            {label: '所属任务名', name: 'taskName', index: 'taskName', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $('#form-TaskWorkitem').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            /*输入框不同状态，显示图片的样式*/
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            /*验证*/
            workitemName: {
                /*键名username和input name值对应*/
                message: '工作项名称校验失败',
                validators: {
                    notEmpty: {
                        /*非空提示*/
                        message: '工作项名称不能为空'
                    },
                    stringLength: {
                        max: 64,
                        message: '输入超出长度'
                    }
                }
            },
            deliveryGoods: {
                message: '交付物描述校验失败',
                validators: {
                    /*notEmpty: {/!*非空提示*!/
                        message: '请填写'
                    },*/
                    stringLength: {
                        max: 200,
                        message: '输入超出长度'
                    }
                }
            }
        }
    });
});

$(function () {
    vm.task()
})

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        baseWorkitemInfo: {},
        taskList: {},
        selected: ''
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.baseWorkitemInfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var bootstrapValidator = $("#form-TaskWorkitem").data('bootstrapValidator');
            bootstrapValidator.validate();
            console.log(bootstrapValidator.isValid());
            if (!bootstrapValidator.isValid()) {
                console.log('验证不通过');
                alert('请输入合法值');
                return false;
            } else {
                var url = vm.baseWorkitemInfo.id == null ? "generator/baseworkiteminfo/save" : "generator/baseworkiteminfo/updateWorkitemInfo";
                console.log(vm.baseWorkitemInfo);
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    headers: {
                        token: token
                    },
                    contentType: "application/json",
                    data: JSON.stringify(vm.baseWorkitemInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "generator/baseworkiteminfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "generator/baseworkiteminfo/info/" + id, function (r) {
                vm.baseWorkitemInfo = r.baseWorkitemInfo;
                console.log(r.baseWorkitemInfo)
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        //查询任务
        task: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "generator/basetaskinfo/allList",
                contentType: "application/json",
                success: function (r) {
                    if (r.code == 0) {
                        vm.taskList = r.list;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        }
    }
});